package com.orphan.marketBack.controller;

import com.orphan.marketBack.po.Purchase;
import com.orphan.marketBack.po.PurchaseGood;
import com.orphan.marketBack.service.PurchaseGoodsService;
import com.orphan.marketBack.service.PurchaseService;
import com.orphan.marketBack.util.DateUtil;
import com.orphan.marketBack.vo.ResultVO;
import com.orphan.util.dto.PurchaseGoodDto;
import com.orphan.util.dto.PurchaseOrderDto;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class PurchaseController {
    @Autowired
    private PurchaseService purchaseService;
    @Autowired
    private PurchaseGoodsService purchaseGoodsService;
    @Autowired
    RabbitTemplate rabbitTemplate;

    @PostMapping("/addPurchase")
    public boolean getPurchase(String purchaseId,String purchaseInfo,String purchaseSize,String purchaseNum){
        boolean flag = false;
        Purchase purchase = new Purchase();
        PurchaseGood goods = new PurchaseGood();
        PurchaseOrderDto pod = new PurchaseOrderDto();
        PurchaseGoodDto pgd = new PurchaseGoodDto();
        //把采购单信息添加到数据库中保存
        purchase.setPurchaseId(purchaseId);

        long l = System.currentTimeMillis();
        String s = DateUtil.timeStamp2Date(String.valueOf(l), null);

        purchase.setPurchaseTime(s);
        purchase.setPurchaseStatus("审核中");

        boolean b1 = purchaseService.insert(purchase);

        //把采购单中的需要采购的商品保存到数据库中
        goods.setPurchaseId(purchaseId);
        goods.setGoodsName(purchaseInfo);
        goods.setGoodsSize(purchaseSize);
        goods.setGoodsNum(purchaseNum);
        boolean b2 = purchaseGoodsService.insert(goods);

        if (b1 && b2){
            flag = true;
        }

        pod.setOrderNum(purchaseId);
        pod.setOrderTime(s);
        pgd.setGoodName(purchaseInfo);
        pgd.setGoodNum(purchaseNum);
        pgd.setGoodSize(purchaseSize);
        List<PurchaseGoodDto> list = new ArrayList<>();
        list.add(pgd);
        pod.setGoods(list);

        rabbitTemplate.convertAndSend("purchaseExchange","purchase",pod);


        return flag;
    }

    @PostMapping("/getGoods")
    public PurchaseGood getGoods(String purchaseId){
        PurchaseGood purchaseGood = purchaseGoodsService.selectGoods(purchaseId);
        return purchaseGood;
    }

    @GetMapping("/getPurchase")
    public ResultVO getPurchase(){
        ResultVO resultVO = purchaseService.selectPurchase();
        return resultVO;
    }

    @PostMapping("/selectPurchase")
    public ResultVO selectPurchase(String purchaseId){
        System.out.println(purchaseId);
        ResultVO resultVO = purchaseService.selectPurchaseById(purchaseId);
        return resultVO;
    }
}
